package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * 提示弹窗，宽高比例是黄金比例。
 * 触摸弹窗外部会自动消失
 * 取消键默认添加了消失事件。确认键需要自己添加事件。
 * Created by 彭治铭 on 2017/7/12.
 */

// AlertDialog.getInstance(this,false,true).setMession("你好").darkmode(false)?.show() // 通过getInstance调用。

public class AlertDialog extends BaseDialog {

    public static AlertDialog myAlertDialog;

    /**
     * 标题和提示信息默认都是空的，需要自己添加。确认事件需要自己添加，取消事件已经自己添加好了可以不用管。
     *
     * @param activity
     * @param dismiss  true 触摸外区，弹窗会消失。 false 触摸外区不会消失。【返回键都会消失】
     * @param lite     true 高度较小【无标题时建议小】，false 高度较大。
     * @return
     */
    public static AlertDialog getInstance(Activity activity, boolean dismiss, boolean lite) {
        if (isfinish(activity, myAlertDialog)) {
            myAlertDialog = new AlertDialog(activity);
        }
        if (lite) {
            alert_dialog.getLayoutParams().width = 500;
            alert_dialog.getLayoutParams().height = 200;
            ProportionUtils.getInstance().adapterView(alert_dialog);
        } else {
            alert_dialog.getLayoutParams().width = 500;
            alert_dialog.getLayoutParams().height = 300;
            ProportionUtils.getInstance().adapterView(alert_dialog);
        }
        myAlertDialog.mDismiss = dismiss;
        return myAlertDialog;
    }

    public AlertDialog(Activity activity) {
        init(activity, R.layout.crown_dialog_alert);
        setNegative("取消", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    static View alert_dialog;
    TextView title, mession, Negative, Positive;

    @Override
    protected void initUI() {
        alert_dialog = findViewById(R.id.crown_alert_dialog);
        title = (TextView) findViewById(R.id.crown_txt_title);
        mession = (TextView) findViewById(R.id.crown_txt_mession);
        Negative = (TextView) findViewById(R.id.crown_txt_Negative);
        Positive = (TextView) findViewById(R.id.crown_txt_Positive);
        alert_dialog.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_dialog_content));
        ProportionUtils.getInstance().adapterView(alert_dialog);
        ProportionUtils.getInstance().adapterTextView(title);
        ProportionUtils.getInstance().adapterTextView(mession);
        ProportionUtils.getInstance().adapterTextView(Negative);
        ProportionUtils.getInstance().adapterTextView(Positive);
    }

    @Override
    protected void setWindowAnimations() {
        //super.setWindowAnimations();
        window.setWindowAnimations(R.style.crown_window_alpha);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return false;
    }

    //标题
    public AlertDialog setTitle(String title) {
        this.title.setText(title);
        return myAlertDialog;
    }

    //信息
    public AlertDialog setMession(String mession) {
        this.mession.setText(mession);
        return myAlertDialog;
    }

    //取消事件
    public AlertDialog setNegative(String Negative, View.OnClickListener listener) {
        this.Negative.setText(Negative);
        this.Negative.setOnClickListener(listener);
        return myAlertDialog;
    }

    //关闭弹窗
    public static void dismisss() {
        if (myAlertDialog != null) {
            myAlertDialog.dismiss();
        }
    }

    //确定事件
    public AlertDialog setPositive(String Positive, View.OnClickListener listener) {
        this.Positive.setText(Positive);
        this.Positive.setOnClickListener(listener);
        return myAlertDialog;
    }

    boolean mDismiss = false;

    @Override
    protected void listener() {
        findViewById(R.id.crown_dialog_content).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (mDismiss && motionEvent.getAction() == MotionEvent.ACTION_DOWN) {//一定要加ACTION_DOWN判断，不然可能出错。
                    dismiss();
                }
                return true;
            }
        });
        alert_dialog.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    @Override
    protected void recycleView() {

    }

    public static void setMyAlertDialog(AlertDialog myAlertDialog) {
        AlertDialog.myAlertDialog = myAlertDialog;
    }

    public static void setAlert_dialog(View alert_dialog) {
        AlertDialog.alert_dialog = alert_dialog;
    }

    public void setTitle(TextView title) {
        this.title = title;
    }

    public void setMession(TextView mession) {
        this.mession = mession;
    }

    public void setNegative(TextView negative) {
        Negative = negative;
    }

    public void setPositive(TextView positive) {
        Positive = positive;
    }

    public void setmDismiss(boolean mDismiss) {
        this.mDismiss = mDismiss;
    }
}
